﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Manager_Home1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        if (!IsPostBack)
        {
            BindMenu();
            lblLoginName.Text = Session["UserName"].ToString();
            tvwmenu.Attributes.Add("onclick", "fnTopPage()");

        }



    }

    private void CheckPage()
    {
        try
        {
            if (System.IO.Path.GetFileName(Request.ServerVariables["HTTP_REFERER"]).ToLower() != "default.aspx")
            {
                if (Request.QueryString["CodeID"] != null && Request.QueryString["CodeID"] != "")
                {
                    if (Request.QueryString["CodeID"] != "0" && Session["IsAdmin"].ToString() != "S" && Session["AutID"].ToString() != "0")
                    {
                        clsGetMenuAuth clsAuth = new clsGetMenuAuth(Session["AdminUserID"].ToString(), Session["AutID"].ToString(), Session["IsAdmin"].ToString(), Request.QueryString["CodeID"]);
                        if (clsAuth.IsShowAuth() == false)
                            Response.Redirect("~/Manager/default.aspx");
                    }
                    else if (Request.QueryString["CodeID"] != "-1" && Session["IsAdmin"].ToString() != "S" && Session["AutID"].ToString() == "0")
                    {
                        Response.Redirect("~/Manager/default.aspx");
                    }
                }
            }
        }
        catch
        {
            Response.Redirect("~/Manager/default.aspx");
        }
    }


    private void BindMenu()
    {
        TreeNode tnInfo = new TreeNode();
        tnInfo.Text = "Home Page";
        tnInfo.ImageUrl = @"~\Images\Icon\Homepage.jpg";
        tnInfo.NavigateUrl = "~/Default.aspx";
        tnInfo.Target = "ifBody";
        tvwmenu.Nodes.Add(tnInfo);

        tnInfo = new TreeNode();
        tnInfo.Text = "Home Admin";
        tnInfo.ImageUrl = @"~\Images\Icon\HomeAdmin.jpg";
        tnInfo.NavigateUrl = "~/Manager/SummaryHome.aspx?CodeID=0";
        tnInfo.Target = "ifBody";
        tvwmenu.Nodes.Add(tnInfo);
        TreeNode childInfo;
        if (Session["IsAdmin"].ToString() != "S")
        {
            tnInfo = new TreeNode();
            tnInfo.Text = "Thông tin cá nhân";
            tnInfo.ImageUrl = @"~\Images\Icon\user.png";
            tvwmenu.Nodes.Add(tnInfo);

            childInfo = new TreeNode();
            childInfo.Text = "Thông tin của bạn";
            childInfo.NavigateUrl = "~/Manager/PrivateInfo/PrivateInfo.aspx?CodeID=0";
            childInfo.Target = "ifBody";
            childInfo.ImageUrl = @"~\Images\Icon\Dummy_user.png";
            tnInfo.ChildNodes.Add(childInfo);

            childInfo = new TreeNode();
            childInfo.Text = "Thay đổi Email";
            childInfo.NavigateUrl = "~/Manager/PrivateInfo/ChangeEmail.aspx?CodeID=0";
            childInfo.Target = "ifBody";
            childInfo.ImageUrl = @"~\Images\Icon\Dummy_user.png";
            tnInfo.ChildNodes.Add(childInfo);

            childInfo = new TreeNode();
            childInfo.Text = "Thay đổi mật khẩu";
            childInfo.NavigateUrl = "~/Manager/PrivateInfo/ChangePassword.aspx?CodeID=0";
            childInfo.Target = "ifBody";
            childInfo.ImageUrl = @"~\Images\Icon\Dummy_user.png";
            tnInfo.ChildNodes.Add(childInfo);
        }
        clsWebShop cls = new clsWebShop();
        DataTable dt = null;
        dt = cls.Get_MenuNoneProc().Tables[0];

        if (dt.Rows.Count > 0)
        {
            foreach (DataRow myRow in dt.Rows)
            {
                if ((myRow["LevelMenuID"].ToString() == "0"))
                {
                    TreeNode tnChild = CreateItem(myRow, "N");
                    if (tnChild != null)
                    {
                        tvwmenu.Nodes.Add(tnChild);
                        PopulateSubMenu(dt, myRow["MenuID"].ToString(), tnChild);
                    }
                }
            }
        }

        if (Session["IsAdmin"].ToString() == "S")
        {

            tnInfo = new TreeNode();
            tnInfo.Text = "Quản lý menu";
            tnInfo.ImageUrl = @"~\Images\Icon\mncon.png";
            // tnInfo.Target = "ifBody";
            tvwmenu.Nodes.Add(tnInfo);

            childInfo = new TreeNode();
            childInfo.Text = "Cập nhật menu";
            childInfo.NavigateUrl = "~/Manager/Menu/Menu.aspx?CodeID=-1";
            childInfo.ImageUrl = @"~\Images\Icon\mnchild.png";
            childInfo.Target = "ifBody";
            tnInfo.ChildNodes.Add(childInfo);

            /* childInfo = new TreeNode();
             childInfo.Text = " Quyền hạn menu";
             childInfo.NavigateUrl = "~/Manager/Menu/MenuD.aspx?index=1&CodeID=-1";
             childInfo.ImageUrl = @"~\Images\Icon\mnchild.png";
             tnInfo.ChildNodes.Add(childInfo);*/

            childInfo = new TreeNode();
            childInfo.Text = "Người dùng menu";
            childInfo.NavigateUrl = "~/Manager/Menu/MenuD.aspx?index=0&CodeID=-1";
            childInfo.ImageUrl = @"~\Images\Icon\mnchild.png";
            childInfo.Target = "ifBody";
            tnInfo.ChildNodes.Add(childInfo);
        }

    }

    private TreeNode CreateItem(DataRow myRow, string leaf_mk)
    {
        bool bcheckItem = false;
        if (Session["IsAdmin"].ToString() == "S")
        {
            bcheckItem = true;
        }
        else
        {
            clsGetMenuAuth clsAuth = new clsGetMenuAuth(Session["AdminUserID"].ToString(), Session["AutID"].ToString(), Session["IsAdmin"].ToString(), Request.QueryString["CodeID"]);
            bcheckItem = clsAuth.IsShowAuth();
        }

        if (bcheckItem)
        {
            TreeNode tnChild = new TreeNode();
            if (leaf_mk == "N")
                tnChild.ImageUrl = @"~\Images\Icon\IconAGroup.png";
            else
                tnChild.ImageUrl = @"~\Images\Icon\iconAchild.png";
            tnChild.Text = myRow["MenuName"].ToString();
            if (myRow["MenuURL"].ToString() != "")
            {
                string sMenuURL = myRow["MenuURL"].ToString();
                if (sMenuURL.IndexOf("?") == -1)
                    tnChild.NavigateUrl = sMenuURL + "?CodeID=" + myRow["MenuID"].ToString();
                else
                    tnChild.NavigateUrl = sMenuURL + "&CodeID=" + myRow["MenuID"].ToString();
            }
            tnChild.Target = "ifBody";
            return tnChild;
        }
        return null;

    }

    private void PopulateSubMenu(DataTable myApMenuData, string menu_id, TreeNode tnChild)
    {

        foreach (DataRow childRow in myApMenuData.Rows)
        {

            if (childRow["LevelMenuID"].ToString() == menu_id)
            {
                string leaf_mk = "Y";
                TreeNode childItem = CreateItem(childRow, leaf_mk);
                if (childItem != null)
                {
                    tnChild.ChildNodes.Add(childItem);
                    PopulateSubMenu(myApMenuData, childRow["MenuID"].ToString(), childItem);
                }


            }
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();
        Session.Abandon();
        Session.Clear();
        Response.Redirect("~/Manager/default.aspx");
    }

    protected void Page_Init(Object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
            Response.Redirect("~/Manager/default.aspx");
    }
}
